package cn.glfs.domain.strategy.service.rule.chain.impl;

import cn.glfs.domain.strategy.model.enums.LogicChainModel;
import cn.glfs.domain.strategy.model.valobj.LogicChainAwardVO;
import cn.glfs.domain.strategy.repository.IStrategyRepository;
import cn.glfs.domain.strategy.service.rule.chain.AbstractLogicChain;
import cn.glfs.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 黑名单规则
 */
@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {

    @Resource
    private IStrategyRepository repository;

    @Override
    protected String ruleModel() {
        return LogicChainModel.RULE_BLACKLIST.getCode();
    }

    @Override
    public LogicChainAwardVO logic(String userId, Long strategyId) {
        String blackConfigValue = repository.queryStrategyRuleValue(strategyId, ruleModel());
        String[] splitRuleValue = blackConfigValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(splitRuleValue[0]);
        String[] blackUserIds = splitRuleValue[1].split(Constants.SPLIT);

        for (String userBlackId : blackUserIds) {
            if (userId.equals(userBlackId)) {
                log.info("抽奖责任链-黑名单接管 userId: {} strategyId: {} ruleModel: {} awardId: {}", userId, strategyId, ruleModel(), awardId);
                return LogicChainAwardVO.builder()
                        .awardId(awardId)
                        .logicModel(ruleModel())
                        .build();
            }
        }

        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        return next().logic(userId, strategyId);
    }
}